package top.lc951.myandroid;

import android.content.Context;
import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import top.lc951.myandroid.adapter.MyRecyclerAdapter;
import top.lc951.myandroid.model.ComingBean;
import top.lc951.myandroid.recelerview.SectionDecoration;

/**
 * RecyclerView的吸顶效果
 * 布局：{@link ConstraintLayout}{@link RecyclerView}
 *
 * 缺陷：吸顶点击回调暂不能实现
 */
public class PullUpRecyclerActivity extends AppCompatActivity {
    RecyclerView recyclerView;

    public static void actionActivity(Context context) {
        Intent intent = new Intent(context, PullUpRecyclerActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pulluprecycler);

        recyclerView = findViewById(R.id.recyclerView);

        createData();
    }

    private void createData() {
        List<ComingBean> comingBeanList = new ArrayList<>();
        List<String> headerList=new ArrayList<>();
        String header="";
        for (int i = 0; i < 30; i++) {
            if(i%5==0){
                header="第"+headerList.size()/5+"组";
            }
            headerList.add(header);
            ComingBean bean = new ComingBean();
            bean.setComingTitle(header);
            bean.setBoxInfo("bean:" + i);
            bean.setNm("bean:" + i);
            comingBeanList.add(bean);
        }
        recyclerView.addItemDecoration(new SectionDecoration(headerList,PullUpRecyclerActivity.this, new SectionDecoration.DecorationCallback() {
            //返回标记id (即每一项对应的标志性的字符串)
            @Override
            public String getGroupId(int position) {
                if(headerList.get(position)!=null) {
                    return headerList.get(position);
                }
                return "-1";
            }

            //获取同组中的第一个内容
            @Override
            public String getGroupFirstLine(int position) {
                if(headerList.get(position)!=null) {
                    return headerList.get(position);
                }
                return "";
            }
        }));
        //解析数据成功,设置适配器
        MyRecyclerAdapter adapter = new MyRecyclerAdapter(this, comingBeanList);
        recyclerView.setAdapter(adapter);

        //recycleView不仅要设置适配器还要设置布局管理者,否则图片不显示
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
    }
}